import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class Cform extends StatefulWidget {
  final List<Map> list;
  Cform({
    super.key,
    required this.list,
  });
  late MyHomePage myHomePage = MyHomePage(list: list);
  @override
  // ignore: no_logic_in_create_state
  State<Cform> createState() => myHomePage;
  void setData(String name, String value) {
    myHomePage.setData(name, value);
  }
}

class MyHomePage extends State<Cform> {
  final List<Map> list;
  MyHomePage({
    required this.list,
  });
  static Map from = {};
  static Map controllerFrom = {};

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [...getFormWidget(list, controllerFrom)],
    );
  }

  void setData(String name, String value) {
    setState(() {
      from[name] = value;
      for (var item in list) {
        if (item['field'] == name) {
          item['value'] = value;
        } else {
          if (controllerFrom[item['field']] != null &&
              controllerFrom[item['field']].text != null) {
            item['value'] = controllerFrom[item['field']].text;
          }
        }
      }
      // ignore: avoid_print
    });
  }
}

List<Widget> getFormWidget(List<Map> list, controllerFrom) {
  List<Widget> Dom = [];
  for (var val in list) {
    if (val['type'] == 'InputText') {
      if (controllerFrom[val['field']] != null) {
        controllerFrom[val['field']].text = val['value'];
      } else {
        controllerFrom[val['field']] = TextEditingController();
        controllerFrom[val['field']].text = val['value'];
      }
      Dom.add(BrnInputText(
        textEditingController: controllerFrom[val['field']],
        maxHeight: 200,
        minHeight: 30,
        minLines: 1,
        maxLength: 10,
        bgColor: Colors.grey[200]!,
        textInputAction: TextInputAction.newline,
        maxHintLines: 20,
        // ignore: prefer_interpolation_to_compose_strings
        hint: '请输入' + val['name'],
        padding: const EdgeInsets.fromLTRB(20, 10, 20, 14),
        onTextChange: (text) {
          if (val['callback'] != null) {
            val['callback'](text);
          }
        },
        onSubmit: (text) {},
      ));
    }
    if (val['type'] == 'input') {
      if (controllerFrom[val['field']] != null) {
        controllerFrom[val['field']].text = val['value'];
      } else {
        controllerFrom[val['field']] = TextEditingController();
        controllerFrom[val['field']].text = val['value'];
      }
      Dom.add(BrnTextInputFormItem(
        title: val['name'],
        controller: controllerFrom[val['field']],
        // ignore: prefer_interpolation_to_compose_strings
        hint: '请输入' + val['name'],
        onChanged: (newValue) {
          if (val['callback'] != null) {
            val['callback'](newValue);
          }
        },
      ));
    }
    if (val['type'] == 'radio') {
      List optionsMap = val['options'];
      List<String> options = [];
      String value = '';
      for (var element in optionsMap) {
        if (val['value'] == element['value']) {
          value = element['name'];
        }

        options.add(element['name']);
      }
      // ignore: avoid_print
      Dom.add(BrnRadioInputFormItem(
        title: val['name'],
        options: options,
        value: value,
        onTip: () {},
        onAddTap: () {},
        onRemoveTap: () {},
        onChanged: (oldValue, newValue) {
          if (val['callback'] != null) {
            String value = '';
            for (var element in optionsMap) {
              if (newValue == element['name']) value = element['value'];
            }
            val['callback'](value);
          }
        },
      ));
    }
  }
  if (Dom.isEmpty) {
    Dom.add(const Text('暂无表单'));
  }
  // ignore: avoid_print
  return Dom;
}
